package com.hongning.railway.ui.main.mine.partymember;

import android.app.Application;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableList;
import android.support.annotation.NonNull;
import android.util.Log;

import com.google.gson.Gson;
import com.hongning.railway.BR;
import com.hongning.railway.R;
import com.hongning.railway.app.Constant;
import com.hongning.railway.data.AppRepository;
import com.hongning.railway.entity.UserEntity;
import com.hongning.railway.entity.reqbean.QuestionResultBean;
import com.hongning.railway.entity.reqbean.ReqAssessesIntegral;
import com.hongning.railway.ui.main.cadremanager.itemviewmodel.CadreManagerItemViewModel;
import com.hongning.railway.ui.main.mine.partymember.itemviewmodel.QuestionItemViewModel;
import com.hongning.railway.util.Apputils;

import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.http.ApiDisposableObserver;
import me.goldze.mvvmhabit.http.BaseResponse;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class QuestionFragmentViewModel extends BaseViewModel<AppRepository> {
    public ItemBinding<QuestionItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_question);
    public ObservableList<QuestionItemViewModel> viewlist = new ObservableArrayList<>();
    public QuestionFragmentViewModel(@NonNull Application application) {
        super(application);

    }

    public QuestionFragmentViewModel(@NonNull Application application, AppRepository model) {
        super(application, model);
    }

    public void requestData(ReqAssessesIntegral reqAssessesIntegral){

        reqAssessesIntegral.setEvaluationUserId(Apputils.getUserId());
        model.selectAssessesIntegral(reqAssessesIntegral)
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        showDialog();
                    }
                }).doOnTerminate(new Action() {
            @Override
            public void run() throws Exception {
                dismissDialog();
            }
        }).subscribe(new ApiDisposableObserver<List<QuestionResultBean>>() {
            @Override
            public void onResult(List<QuestionResultBean> questionResultBeans) {
                if (questionResultBeans==null|| questionResultBeans.isEmpty())
                {
                    ToastUtils.showShort("无数据");
                    return;
                }
                if (viewlist!=null)
                {
                    viewlist.clear();
                }
                for (QuestionResultBean questionResultBean:questionResultBeans)
                {
                    viewlist.add(new QuestionItemViewModel(QuestionFragmentViewModel.this,questionResultBean));
                }

            }
        });

    }
}
